function showPurchase() {
    layui.use('layer',function () {
        var layer = layui.layer;
        layer.open({
            type: 2,
            area: ['800px','350px'],
            content: 'deliveryOrder_purchaseOrder.html'
        })
    });
}

function getPurchase(res) {
    $("#purchaseOrderCode").val(res[0].purchaseOrderCode);
    $("#purchaseOrderId").val(res[0].id)
}

function show() {
    layui.use('layer',function () {
        var layer = layui.layer;
        layer.open({
            type: 2,
            area: ['800px','350px'],
            content: 'deliveryOrder_provider.html'
        })
    });
}
function get(res) {
    $("#supplierId").val(res[0].supplierId)
    $("#supplierName").val(res[0].supplierName)
}
// layui.use('table',function () {
//     var table = layui.table;
// });
function addTr(Obj){
    $(Obj).parents('tr').after("<tr>\n" +
        "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 60px\">\n" +
        "                        <div class=\"layui-table-cell\" style=\"padding-bottom: 0;padding-top: 0\">\n" +
        "                            <button type=\"button\" name=\"addNewLine_btn\" class=\"layui-btn layui-btn-normal layui-btn-xs addNewLine_btn\" onclick=\"addTr(this)\">+</button>\n" +
        "                            <button type=\"button\" name=\"delNowLine_btn\" class=\"layui-btn layui-btn-danger layui-btn-xs delNowLine_btn\" onclick=\"delTr(this)\">×</button>\n" +
        "                        </div>\n" +
        "                    </td>\n" +
        "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 480px\" onclick=\"showGoodsInput(this)\">\n" +
        "                        <div class=\"layui-table-cell selGoods\" style=\"padding-bottom: 0;padding-top: 0\"></div>\n" +
        "                        <input type=\"hidden\" name=\"goodsId\" value=\"\">\n" +
        "                    </td>\n" +
        "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 260px\" onclick=\"showQuantityInput(this)\">\n" +
        "                        <div class=\"layui-table-cell\" style=\"padding-bottom: 0;padding-top: 0\"></div>\n" +
        "                    </td>\n" +
        "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 100px\">\n" +
        "                        <div class=\"layui-table-cell\" style=\"padding-bottom: 0;padding-top: 0\"></div>\n" +
        "                    </td>\n" +
        "                </tr>")
}

function delTr(Obj){
    var layer = layui.layer;
    if($(Obj).parents("table").find("tr").length-1<=1){
        layer.msg("最后一行不可删除哦！")
    }else {
        $(Obj).parents('tr').remove();
    }
}

function showGoodsInput(Obj){
    var trArray = document.getElementsByTagName("tr");
    for(var i =1;i<trArray.length;i++){
        var tdArray = trArray[i].getElementsByTagName("td");
        var inputArray = tdArray[1].getElementsByTagName("input");
        hideGoodsInput(inputArray[1]);
        var inputArray1 = tdArray[2].getElementsByTagName("input");
        hideQuantityInput(inputArray1[0]);
    }
    // for(var j =1;i<trArray.length;i++){
    //     var tdArray1 = trArray[j].getElementsByTagName("td");
    //     console.log(trArray.length);
    //
    // }
    var n = $(Obj).find("div:first").text();
    $(Obj).find("div:first").hide();
    var a = $(Obj).find(".inputText").val();
    if(a===undefined){
        $(Obj).append("<div class=\"layui-inline\"><input type=\"text\" class=\"layui-input inputText\"  value=\""+n+"\" onblur=\"hideGoodsInput(this)\"></div>");
        $(Obj).append("<div class=\"layui-inline\"><input type=\"button\" class=\"layui-btn layui-btn-sm\" value=\"选择\" onclick=\"addGoods(this)\"></div>");
    }
}


function hideGoodsInput(Obj){
    var x = $(Obj).val();
    $(Obj).parent("div").prevAll("div").html(x);
    $(Obj).parent("div").nextAll("div").remove();
    $(Obj).parent("div").prevAll("div").show();
    $(Obj).parent("div").remove();
}

function showQuantityInput(Obj){

    $(Obj).find("div:first").hide();
    var b = $(Obj).find(".inputText").val();
    var m = $(Obj).find("div:first").text();
    if(b===undefined){
        $(Obj).append("<div class=\"layui-inline\"><input type=\"text\" name='number' class=\"layui-input inputText\" value=\""+m+"\" onblur=\"hideQuantityInput(this)\"></div>");
        $(Obj).find(".inputText").focus();
    }
}
function hideQuantityInput(Obj){
    var y = $(Obj).val();
    $(Obj).parent("div").prevAll("div").html(y);
    $(Obj).parent("div").prevAll("div").show();
    $(Obj).parent("div").remove();
}
var ssthis ="";
//获得商品对象后加入到列中
function addGoods(obj){
    ssthis = obj;
    layui.use('layer',function () {
        var layer = layui.layer;
        layer.open({
            type: 2,
            area: ['800px','380px'],
            content: 'purchase_goods.html', //这里content是一个普通的String
            success:function (layero,index) {
                var iframe = window['layui-layer-iframe'+index]
                iframe.tes();
            }
        });
    })
}

function jzgoods(dataa) {
    var Obj = ssthis;
    var a = [];
    var goodName = [];
    goodName = document.getElementsByTagName("tr");
    for (var i = 0; i <dataa.length ; i++) {
        var flag = false;
        for (var j = 1; j <goodName.length ; j++) {
            var x = $(goodName[j]).find("[name='goodsId']").val();
            if (x==dataa[i].id){
                flag = true;
            }
        }
        if (!flag){
            a.push(dataa[i])
        }
    }
    //获得的对象
    var b = "";
    for(var i=0;i<a.length;i++){
        b = b +"<tr>\n" +
            "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 60px\">\n" +
            "                        <div class=\"layui-table-cell\" style=\"padding-bottom: 0;padding-top: 0\">\n" +
            "                            <button type=\"button\" name=\"addNewLine_btn\" class=\"layui-btn layui-btn-normal layui-btn-xs addNewLine_btn\" onclick=\"addTr(this)\">+</button>\n" +
            "                            <button type=\"button\" name=\"delNowLine_btn\" class=\"layui-btn layui-btn-danger layui-btn-xs delNowLine_btn\" onclick=\"delTr(this)\">×</button>\n" +
            "                        </div>\n" +
            "                    </td>\n" +
            "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 480px\" onclick=\"showGoodsInput(this)\">\n" +
            "                        <div class=\"layui-table-cell selGoods\" style=\"padding-bottom: 0;padding-top: 0\">"+ a[i].goodsName +"</div>\n" +
            "                        <input type=\"hidden\" name=\"goodsId\" value=\""+ a[i].id +"\">\n" +
            "                    </td>\n" +
            "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 260px\" onclick=\"showQuantityInput(this)\">\n" +
            "                        <div class=\"layui-table-cell\" name='cNumber' style=\"padding-bottom: 0;padding-top: 0\"></div>\n" +
            "                    </td>\n" +
            "                    <td style=\"padding-bottom: 5px;padding-top: 5px;width: 100px\">\n" +
            "                        <div class=\"layui-table-cell\" name='price' style=\"padding-bottom: 0;padding-top: 0\">"+ a[i].salesPrice +"</div>\n" +
            "                    </td>\n" +
            "                </tr>"
    }
    $('table>tbody').prepend(b);
    $(Obj).parents('tr').remove();
}
function showInfo(){
    layui.use('layer',function () {
        var layer = layui.layer;
        if($("#supplierId").val()!=''&&$("#supplierName").val()!=''){
            var purchaseOrderId = $("#purchaseOrderId").val();
            var purchaseOrderCode = $("#purchaseOrderCode").val();
            if (purchaseOrderId!=''&&purchaseOrderCode!=''){
                var goodName = [];
                var list = [];
                var flag = false;
                var zzbds = true;
                var total = 0;
                goodName = document.getElementsByTagName("tr");
                for (var i = 1; i <goodName.length ; i++) {
                    var x = $(goodName[i]).find("[name='goodsId']").val();
                    if ($(goodName[i]).find("[name='goodsId']").val()!=''){
                        if ($(goodName[i]).find("[name='cNumber']").text()==''){
                            flag = true;
                        }else {
                            var reg = /^[1-9]\d*$/;
                            if (!reg.test($(goodName[i]).find("[name='cNumber']").text())){
                                zzbds = false;
                                layer.msg("数量只能为正整数")
                                break;
                            }else {
                                total= total+parseInt($(goodName[i]).find("[name='cNumber']").text())*$(goodName[i]).find("[name='price']").text()
                            }
                        }
                        var purchaseorderDetail = {
                            goodsId: $(goodName[i]).find("[name='goodsId']").val(),
                            quantityOfGoods: $(goodName[i]).find("[name='cNumber']").text()
                        }
                        list.push(purchaseorderDetail);
                    }
                }
                if (zzbds){
                    if (list.length<=0){
                        layer.msg("采购单中没有商品")
                    }else {
                        if (flag){
                            layer.msg("商品采购数量必填")
                        }
                    }
                }
                if (!flag&&zzbds){
                    var token = getCookie("token");
                    $.post('htz/user/token',{token,token},function (res) {
                        if (res!=null){
                            var userId = res.id;
                            var purchaseUtil = {
                                list: list,
                                total : total,
                                userId: userId,
                                purchaseOrderCode: $("#purchaseOrderCode").val(),
                                purchaseId: $("#purchaseOrderId").val(),
                                supplierId: $("#supplierId").val()
                            }
                            $.ajax({
                                url: "pk/deliveryOrder/addDeliveryOrder",
                                data: JSON.stringify(purchaseUtil),
                                contentType: "application/json;charset=UTF-8",
                                type: "post",
                                dataType: "json",
                                success: function (res) {
                                    if(res.obj>0){
                                        layer.msg("货验单新增成功")
                                        setTimeOut(function () {
                                            window.location.href="deliveryOrder_add.html";
                                        },1000)
                                    }
                                }
                            })
                        }
                    })
                }
            } else {
                layer.msg("货验单不能为空")
            }
        }else {
            layer.msg("提供商不能为空")
        }
    })

}